<?php
	require_once "Settings.php";
	
	class DBConnection {
		
		private $m_mysqli = NULL;
		
		//Skapar en uppkoppling
        public function Connect() {
			$password = "";
			$this->m_mysqli = new mysqli(Settings::DBHOST,
										 Settings::DBUSER,
										 Settings::DBPASSWORD,
										 Settings::DATABASE);
                  
			//Sätter charset innan...
			$this->m_mysqli->set_charset("utf8");
              
          	//Kollar uppkopplingen
          	if ($this->m_mysqli->connect_errno) {
          		echo "Connect failed: $this->m_mysqli->connect_error";
          		return FALSE;
			}
			return TRUE;
		}
		
		//Stänger uppkopplingen	
		public function Close() {
			echo "Close Connection";
			$this->m_mysqli->close();
		}
		
		//Returnerar ett mysql stmt
        public function Prepare($sql) {
        	$stmt = $this->m_mysqli->prepare($sql);
        	if ($stmt === FALSE) {
        		echo "prepare of '$sql' failed " . $this->m_mysqli->error;
        		return FALSE;
			}
			return $stmt;
        }
		
        //Returnerar en array av instanser med Primery Key som nyckel.
        public function RunAndFetchObjects(mysqli_stmt $a_stmt, $a_className, $a_nameOfPrimaryKey) {
        	$ret = array();
        	
        	$a_stmt->execute();
        	
        	$result = $a_stmt->get_result();
        	
        	while ($object = $result->fetch_object($a_className)) {
        			
        		$ret[$object->$a_nameOfPrimaryKey] = $object;
			}
			
			$a_stmt->close();
			
			return $ret;
        }
    }

?>